<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.ccams.util.*"%>
<%
boolean success = true;
		String message = "";
		float maxWidthSize = Float.parseFloat(OperatePropertiesFile
				.getValueByPropertyName_("uploadFile", "maxWidthSize"));
		float maxHeightSize = Float.parseFloat(OperatePropertiesFile
				.getValueByPropertyName_("uploadFile", "maxHeightSize"));
		String rootPath = request.getSession().getServletContext().getRealPath(
				"/");
		String savePath = rootPath
				+ OperatePropertiesFile.getValueByPropertyName_("uploadFile",
						"savePath");
		FileUtils.mkDirectory(savePath);
		String allowType = OperatePropertiesFile.getValueByPropertyName_(
				"uploadFile", "allowedTypes");
		long size = 0;// 附件大小
		long maxFileSize = Long.parseLong(OperatePropertiesFile
				.getValueByPropertyName_("uploadFile", "allowedMaxSize")) * 1024 * 1024;
		String filename = "";// 文件名
		String oldfilename = "";// 文件原名
		String ext_name = "";// 文件扩展名
		InputStream is = null;// 输入流
		DiskFileUpload upload = new DiskFileUpload();
		upload.setSizeMax(1024 * 1024 * 20);
		Iterator iter = upload.parseRequest(request).iterator();
		while (iter.hasNext()) {
			FileItem item = (FileItem) iter.next();
			if (!item.isFormField()) { // 如果是文件域表单
				is = item.getInputStream();// 获得上传文件的输入流
				size = item.getSize();// 文件大小
				if (size > maxFileSize) {
					success = false;
					message = "文件大小超过了限制";
				}
				filename = item.getName();
				filename = filename.substring(filename
						.lastIndexOf(File.separator) + 1);				
				ext_name = filename.substring(filename.length() - 3, filename
						.length());
				oldfilename = java.net.URLEncoder.encode(filename.replaceAll(ext_name, ""),"UTF-8");
				ext_name = ext_name.toLowerCase();
				if (!allowType.contains(ext_name)) {
					success = false;
					message = "这种文件类型不允许上传";
				}
			}
		}
		Random r = new Random();
		int rand = r.nextInt(100000000);
		// 生成新文件名
		String newFileName = rand + "." + ext_name;
		// 新文件全路径
		String filePath = savePath + newFileName;
		if(success){
			// 往服务器指定目录写文件
			FileOutputStream output = new FileOutputStream(filePath);
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = is.read(b)) != -1) {
				output.write(b, 0, len);
			}
			output.flush();
			output.close();	
			FileUtils.createMiniPic(new File(savePath + newFileName), maxWidthSize,
					maxHeightSize);
			message = newFileName;
		}		
		is.close();	
		out.print("{success:"+success+",message:'"+message+"'}");
%>
